package leetcode.offer;

import leetcode.base.ListNode;

/**
 * ClassName: MergeTwoLists
 * Description: leetcode-剑指offer 25.合并两个排序的列表
 * date: 2021-11-08 22:45
 *
 * @author liyifan
 */
public class MergeTwoLists {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null)
            return l2;
        if (l2 == null)
            return l1;

        ListNode head = new ListNode(0);
        ListNode p = head;
        while (l1 != null && l2 != null) {
            if (l1.val > l2.val) {
                p.next = new ListNode(l2.val);
                l2 = l2.next;
                p = p.next;
            } else {
                p.next = new ListNode(l1.val);
                l1 = l1.next;
                p = p.next;
            }
        }
        if (l1 == null)
            p.next = l2;
        if (l2 == null)
            p.next = l1;
        return head.next;
    }
}
